home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / UTILITIE / CPU_MEMO / 3468.ZIP / POPUP.ZIP / GETSTR.C < prev    next >
C/C++ Source or Header  |  1988-06-30  |  5KB  |  142 lines

  1. /*****************************************************************************
  2. * Module       : GETSTR.C                                                    *
  3. *----------------------------------------------------------------------------*
  4. * Program      : POPUP LIBRARY                                               *
  5. *----------------------------------------------------------------------------*
  6. * Author       : M.R.Watson.    Copyright (c) M.R.Watson 1988                *
  7. *----------------------------------------------------------------------------*
  8. * Last Updated : 24/06/88                                                    *
  9. *----------------------------------------------------------------------------*
  10. *                                 Synopsis                                   *
  11. *                                 ========                                   *
  12. * Auxilliary keyboard input functions to complement the screen functions     *
  13. * used in the popup library. These functions must only use BIOS routines.    *
  14. * DOS keyboard and screen routines are taboo in popup programs!              *
  15. * (But file handling DOS functions can be used.)                             *
  16. *****************************************************************************/
  17.  
  18. #include <stdio.h>
  19. #include <dos.h>
  20. #include <standard.h>
  21.  
  22. #define BKSP    8
  23. #define RET     13
  24. #define ESC     27
  25.  
  26.  
  27. /*****************************************************************************
  28. * int GetString( int y, int x, char *buffer, int length, int attr )          *
  29. *----------------------------------------------------------------------------*
  30. * Gets a string from the user at the given location, using BIOS only.        *
  31. * If the ESC key is pressed, ESC is returned, else RET is returned           *
  32. *****************************************************************************/
  33.  
  34. GetString( int y, int x, char *buffer, int length, int attr )
  35. {
  36.     int     i, c    ;
  37.     int     sx, sy  ;
  38.  
  39.     ClearBox( y, x, y, x+length-1, attr );
  40.     GetCursor(&sy,&sx);
  41.     i = 0 ;
  42.  
  43.     FOREVER
  44.     {
  45.         SetCursor(y,x+i);
  46.         c = getkey();
  47.         
  48.         if (c == ESC)                       /* Escape key pressed */
  49.         {
  50.             SetCursor(sy,sx);
  51.             return(ESC);
  52.         }
  53.         else if (c == RET)                  /* Return key pressed */
  54.         {
  55.             SetCursor(sy,sx);
  56.             buffer[i] = EOS ;               /* Terminate string entered */
  57.             return(RET);
  58.         }
  59.         else if (c == BKSP)                 /* Backspace key pressed */
  60.         {
  61.             if (--i < 0)
  62.                 i = 0 ;
  63.  
  64.             CharAt( y, x+i, ' ', attr );    /* Erase last character */
  65.             buffer[i] = EOS ;               /* Shorten buffer string */
  66.         }
  67.         else if (c < 256)                   /* Character to go in buffer */
  68.         {
  69.             if ( i < length )               /* If there's room left */
  70.             {
  71.                 buffer[i] = c ;
  72.                 CharAt( y, x+i, c, attr );
  73.                 i++ ;
  74.             }
  75.             else
  76.                 bell();
  77.         }
  78.     }
  79. }
  80.  
  81.  
  82. /*****************************************************************************
  83. * SetCursor( int y, int x )                                                  *
  84. *----------------------------------------------------------------------------*
  85. * Sets the cursor position to row = y, column = x.                           *
  86. *****************************************************************************/
  87.  
  88. SetCursor( int y, int x )
  89. {
  90.     union REGS  r   ;
  91.  
  92.     r.h.ah = 2 ;
  93.     r.h.dh = y ;
  94.     r.h.dl = x ;
  95.     r.h.bh = 0 ;
  96.     int86( 0x10, &r, &r );
  97. }
  98.  
  99.  
  100. /*****************************************************************************
  101. * GetCursor( int *y, int *x )                                                *
  102. *----------------------------------------------------------------------------*
  103. * Gets the cursor position into row = y, column = x.                         *
  104. *****************************************************************************/
  105.  
  106. GetCursor( int *y, int *x )
  107. {
  108.     union REGS  r   ;
  109.  
  110.     r.h.ah = 3 ;
  111.     r.h.bh = 0 ;
  112.     int86( 0x10, &r, &r );
  113.     *x = r.h.dl ;
  114.     *y = r.h.dh ;
  115. }
  116.  
  117. /*****************************************************************************
  118. * void bell(void) : Bleep using the BIOS.                                    *
  119. *****************************************************************************/
  120.  
  121. bell()
  122. {
  123.     union REGS  r   ;
  124.  
  125.     r.x.ax = 0x0E07 ;
  126.     int86( 0x10, &r, &r );
  127. }
  128.  
  129.  
  130. /*****************************************************************************
  131. * void CharAt( int y, int x, int c, int attr )                               *
  132. *----------------------------------------------------------------------------*
  133. * Displays the character 'c' at row 'y', column 'x', using attribute 'attr'. *
  134. *****************************************************************************/
  135.  
  136. CharAt( int y, int x, int c, int attr )
  137. {
  138.     c |= (attr << 8);
  139.     WriteScreen( y, x, &c, 1 );
  140. }
  141.  
  142.